import { input } from "cc"

//事件类型
export const EventName = {
    onCurrentPage: "onCurrentPage",
}

export default {
    handlesMap: {},
    on(eventName: string, callback: Function, target?: any) {
        this.handlesMap[eventName] = this.handlesMap[eventName] || [];
        this.handlesMap[eventName].push({ callback, target });
    },
    off(eventName: string, callback: Function, target?: any) {
        let handles = this.handlesMap[eventName];
        if (handles) {
            if (!callback && !target) {
                handles.length = 0; // 清空
            } else {
                for (let i = handles.length - 1; i >= 0; i--) {
                    if (handles[i].callback === callback && handles[i].target === target) {
                        handles.splice(i, 1);
                    }
                }
            }
        }
    },
    emit(eventName: string, ...data: any) {
        let handles = this.handlesMap[eventName];
        if (handles) {
            handles.forEach(e => {
                e.callback.apply(e.target, data);
            });
        }
    },
}